using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Devices;

namespace myconsole3;

public class ConverterPdf
{
    /// <summary>
    /// 转换为图片的方法
    /// 你可以像这样调用该方法： ToImage("D:\\PdfFile.pdf", "D:\\ImageFile", 1, 20, ImageFormat.Png, 256);
    /// </summary>
    /// <param name="sourcePath"></param>
    /// <param name="targetPath">
    /// 图片输出路径，如果为空，默认值为Word所在路径
    /// </param>
    /// <param name="startPageNum">
    /// 从PDF文档的第几页开始转换，如果为0，默认值为1
    /// </param>
    /// <param name="endPageNum">
    /// 从PDF文档的第几页开始停止转换，如果为0，默认值为Word总页数
    /// </param>
    /// <param name="imageFormat">
    /// 设置所需图片格式，如果为null，默认格式为JPEG
    /// </param>
    /// <param name="resolution">
    /// 设置图片的像素，数字越大越清晰，如果为0，默认值为128，建议最大值不要超过1024
    /// </param>
    public static List<string> ToImage(
        string sourcePath,
        string targetPath,
        int startPageNum = 1,
        int endPageNum = int.MaxValue,
        ImageFormat imageFormat = null,
        int resolution = 128
    )
    {
        SetLicense();
        string imageName = Path.GetFileNameWithoutExtension(sourcePath).Replace(" ", "");
        // 返回的图片绝对路径集合
        List<string> images = new List<string>();
        try
        {
            if (!Directory.Exists(targetPath))
            {
                if (targetPath != null)
                {
                    Directory.CreateDirectory(targetPath);
                }
            }

            using (var pdfDocument = new Document(sourcePath))
            {
                if (startPageNum <= 0)
                {
                    startPageNum = 1;
                }

                if (endPageNum > pdfDocument.Pages.Count || endPageNum <= 0)
                {
                    endPageNum = pdfDocument.Pages.Count;
                }

                if (startPageNum > endPageNum)
                {
                    startPageNum = endPageNum;
                    endPageNum = startPageNum;
                }

                if (imageFormat == null)
                {
                    imageFormat = ImageFormat.Jpeg;
                }

                if (resolution <= 0)
                {
                    resolution = 128;
                }

                for (int i = startPageNum; i <= endPageNum; i++)
                {
                    using (var imageStream = new MemoryStream())
                    {
                        var jpegDevice = new JpegDevice(new Resolution(resolution), 100);
                        jpegDevice.Process(pdfDocument.Pages[i], imageStream);

                        imageStream.Position = 0;

                        using (var image = new Bitmap(imageStream))
                        {
                            if (targetPath == null)
                                continue;
                            string pathAll = Path.Combine(
                                targetPath,
                                imageName + "_" + i + "." + imageFormat.ToString().ToLower()
                            );
                            images.Add(pathAll);
                            image.Save(pathAll, imageFormat);
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }

        return images;
    }

    private static void SetLicense()
    {
        // new License().SetLicense(
        //     new MemoryStream(
        //         Convert.FromBase64String(
        //             "PExpY2Vuc2U+CiAgPERhdGE+CiAgICA8TGljZW5zZWRUbz5TdXpob3UgQXVuYm94IFNvZnR3YXJlIENvLiwgTHRkLjwvTGljZW5zZWRUbz4KICAgIDxFbWFpbFRvPnNhbGVzQGF1bnRlYy5jb208L0VtYWlsVG8+CiAgICA8TGljZW5zZVR5cGU+RGV2ZWxvcGVyIE9FTTwvTGljZW5zZVR5cGU+CiAgICA8TGljZW5zZU5vdGU+TGltaXRlZCB0byAxIGRldmVsb3BlciwgdW5saW1pdGVkIHBoeXNpY2FsIGxvY2F0aW9uczwvTGljZW5zZU5vdGU+CiAgICA8T3JkZXJJRD4xOTA4MjYwODA3NTM8L09yZGVySUQ+CiAgICA8VXNlcklEPjEzNDk3NjAwNjwvVXNlcklEPgogICAgPE9FTT5UaGlzIGlzIGEgcmVkaXN0cmlidXRhYmxlIGxpY2Vuc2U8L09FTT4KICAgIDxQcm9kdWN0cz4KICAgICAgPFByb2R1Y3Q+QXNwb3NlLlRvdGFsIGZvciAuTkVUPC9Qcm9kdWN0PgogICAgPC9Qcm9kdWN0cz4KICAgIDxFZGl0aW9uVHlwZT5FbnRlcnByaXNlPC9FZGl0aW9uVHlwZT4KICAgIDxTZXJpYWxOdW1iZXI+M2U0NGRlMzAtZmNkMi00MTA2LWIzNWQtNDZjNmEzNzE1ZmMyPC9TZXJpYWxOdW1iZXI+CiAgICA8U3Vic2NyaXB0a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